package ro.ciacob.utils {
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import mx.formatters.DateFormatter;
	public class Time {
		public static const COUNTER_FORMAT:String = 'JJ:NN:SS';
		public static const DATESTAMP_FORMAT:String = 'YYYY/MM/DD';
		public static const DEFAULT_DELAY_TIME:int = 20;
		public static const DEFAULT_SPLASH_TIME:Number = 3;
		public static const LONG_DURATION:Number = 1400;
		public static const SHORT_DURATION:Number = 550;
		public static const TIMESTAMP_DEBUG_HEADER:String = 'YYYY/MM/DD JJ:NN:SS';
		public static const TIMESTAMP_DEBUG_INLINE:String = 'JJ:NN:SS';
		public static const TIMESTAMP_DEFAULT:String = 'D MMM YYYY, J:NN';
		public static const TIMESTAMP_FILENAME:String = 'D.MMM.YYYY J.NN.SS';
		public static const TIMESTAMP_LOCAL:String = 'D MMM YYYY, L:NN A';
		public static const YEAR_FORMAT:String = 'YYYY';
		private static var _dateFormatter:DateFormatter = new DateFormatter;

		public static function advancedDelay(action:Function, context:Object, milliseconds:int =
			DEFAULT_DELAY_TIME, ... parameters:Array):void {
			var timer:Timer = new Timer(milliseconds, 1);
			var tmp:Function = function(event:TimerEvent):void {
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE, tmp);
				action.apply(context, parameters);
			}
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, tmp);
			timer.start();
		}

		public static function delay(seconds:Number, action:Function):void {
			advancedDelay(action, {}, seconds * 1000);
		}

		public static function get now():Date {
			return (new Date);
		}

		public static function get timestamp():String {
			_dateFormatter.formatString = TIMESTAMP_DEFAULT;
			return _dateFormatter.format(now);
		}

		public static function toDefaultFormat(time:*):String {
			if (time is String) {
				time = new Date(time);
			}
			if (time is Number) {
				if (isNaN(time)) {
					time = 0;
				}
				time = new Date(time)
			}
			return _dateFormatter.format(time);
		}

		public static function toFormat(date:Date, format:String, removeLocalOffset:Boolean =
			true):String {
			if (removeLocalOffset) {
				var offset:Number = date.getTimezoneOffset();
				date = new Date(date.getTime() + offset * 60000);
			}
			var formatter:DateFormatter = new DateFormatter;
			formatter.formatString = format;
			return formatter.format(date);
		}
	}
}
